package TwoPointers;

public class _713_SubarrayProductLessThanK {
    //over time limited
    public int numSubarrayProductLessThanK_1(int[] nums, int k) {
        int res = 0;
        int product = 1;
        for (int i = 0; i < nums.length; i++) {
            product = 1;
            int temp = i;
            while (temp < nums.length) {
                product *= nums[temp++];
                if (product < k) {
                    res++;
                } else {
                    break;
                }
            }
        }
        return res;
    }

    public int numSubarrayProductLessThanK(int[] nums, int k) {
        if (k <= 0) {
            return 0;
        }
        int res = 0;
        int left = 0;
        int product = 1;
        for (int i = 0; i < nums.length; i++) {
            product *= nums[i];
            while (product >= k && left <= nums.length) {
                product /= nums[left++];
            }
            res += i - left + 1;

        }
        return res;
    }
}
